package com.borya.action.web;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSONObject;
import com.borya.action.BaseAction;
import com.borya.framework.annotation.Action;
import com.borya.framework.annotation.Autowired;
import com.borya.framework.annotation.JsonResult;
import com.borya.framework.annotation.UrlMapping;
import com.borya.framework.core.ServerRequest;
import com.borya.framework.core.ServerResponse;
import com.borya.model.entity.MerchantModelInfo;
import com.borya.model.entity.ModelDetail;
import com.borya.service.web.SourceModelService;
import com.borya.statuscode.StatusCode;

@UrlMapping("/w/model/")
@Action
public class SourceModelAction extends BaseAction{

	
	private Logger log = Logger.getLogger(getClass());
	
	@Autowired("sourceModelServiceImpl")
	private SourceModelService sourceModelService;

	public SourceModelService getSourceModelService() {
		return sourceModelService;
	}

	public void setSourceModelService(SourceModelService sourceModelService) {
		this.sourceModelService = sourceModelService;
	}
	
	@UrlMapping("merchantInfo") 
	@JsonResult
	public String merchantInfo(ServerRequest request,ServerResponse response) {
		String result;
		try {
			JSONObject json = getJson(request);
			final String dealerId = json.getString("dealerId");
			final int type = json.getIntValue("type");
			if(StringUtils.isBlank(dealerId)
					|| type > 2
					|| type < 1){
				log.warn("param error,"+json);
				return StatusCode.PARAM_ERROR.toJSON();
			}
			result = getSourceModelService().merchantInfo(dealerId, type);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result = StatusCode.SYSTEM_EXCEPTION.toJSON();
		}
		log.info("merchantInfo,OUT[" + result + "]");
		return result;
	}
	
	@UrlMapping("modelList") 
	@JsonResult
	public String modelList(ServerRequest request,ServerResponse response) {
		String result;
		try {
			result = getSourceModelService().modelList();
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result = StatusCode.SYSTEM_EXCEPTION.toJSON();
		}
		log.info("modelList,OUT[" + result + "]");
		return result;
	}
	
	@UrlMapping("updMerchant") 
	@JsonResult
	public String updMerchant(ServerRequest request,ServerResponse response) {
		String result;
		try {
			JSONObject json = getJson(request);
			final String dealerId = json.getString("dealerId");
			final int type = json.getIntValue("type");
			if(StringUtils.isBlank(dealerId)
					|| type > 2
					|| type < 1){
				log.warn("param error,"+json);
				return StatusCode.PARAM_ERROR.toJSON();
			}
			MerchantModelInfo info = JSONObject.parseObject(json.toJSONString(),MerchantModelInfo.class);
			result = getSourceModelService().updMerchant(info,type);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result = StatusCode.SYSTEM_EXCEPTION.toJSON();
		}
		log.info("updMerchant,OUT[" + result + "]");
		return result;
	}
	
	@UrlMapping("detailList") 
	@JsonResult
	public String detailList(ServerRequest request,ServerResponse response) {
		String result = getSourceModelService().detailList();
		log.info("detailList,OUT[" + result + "]");
		return result;
	}
	
	@UrlMapping("addDetail") 
	@JsonResult
	public String addDetail(ServerRequest request,ServerResponse response) {
		String result;
		try {
			JSONObject json = getJson(request);
			final int modelType = json.getIntValue("modelType");
			if(modelType > 3
					|| modelType < 1){
				log.warn("param modelType error");
				return StatusCode.PARAM_ERROR.toJSON("模式类型错误");
			}
			ModelDetail detail = JSONObject.parseObject(json.toJSONString(),ModelDetail.class);
			result = getSourceModelService().addDetail(detail,modelType);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result = StatusCode.PARAM_ERROR.toJSON();
		}
		log.info("OUT[" + result + "]");
		return result;
	}
	
	@UrlMapping("detail") 
	@JsonResult
	public String detail(ServerRequest request,ServerResponse response) {
		String result;
		try {
			JSONObject json = getJson(request);
			final int modelId = json.getIntValue("modelId");
			final int modelType = json.getIntValue("modelType");
			if(modelId < 1
					|| modelType > 3
					|| modelType < 1){
				log.warn("param error,"+json);
				return StatusCode.PARAM_ERROR.toJSON();
			}
			result = getSourceModelService().detail(modelId, modelType);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result = StatusCode.PARAM_ERROR.toJSON();
		}
		log.info("OUT[" + result + "]");
		return result;
	}
	
	@UrlMapping("updDetail") 
	@JsonResult
	public String updDetail(ServerRequest request,ServerResponse response) {
		String result;
		try {
			JSONObject json = getJson(request);
			final int modelType = json.getIntValue("modelType");
			final int order = json.getIntValue("order");
			if(modelType > 3
					|| modelType < 1){
				log.warn("param modelType error");
				return StatusCode.PARAM_ERROR.toJSON("模式类型错误");
			}
			if(order < 1){
				log.warn("param order error");
				return StatusCode.PARAM_ERROR.toJSON("序号错误");
			}
			ModelDetail detail = JSONObject.parseObject(json.toJSONString(),ModelDetail.class);
			result = getSourceModelService().updDetail(detail,modelType);
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result = StatusCode.PARAM_ERROR.toJSON();
		}
		log.info("OUT[" + result + "]");
		return result;
	}
	
	@UrlMapping("modelDetailList") 
	@JsonResult
	public String modelDetailList(ServerRequest request,ServerResponse response) {
		String result;
		try {
			JSONObject json = getJson(request);
			final int modelType = json.getIntValue("modelType");
			if(modelType > 3 
					|| modelType < 1){
				log.warn("param modelType error");
				return StatusCode.PARAM_ERROR.toJSON("模式类型错误");
			}
			result = getSourceModelService().modelDetailList(modelType);
			
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result = StatusCode.PARAM_ERROR.toJSON();
		}
		log.info("OUT[" + result + "]");
		return result;
	}
	
	@UrlMapping("cityList") 
	@JsonResult
	public String cityList(ServerRequest request,ServerResponse response) {
		String result;
		try {
			result = getSourceModelService().getCityList();
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result = StatusCode.PARAM_ERROR.toJSON();
		}
		log.info("OUT[" + result + "]");
		return result;
	}
	
	@UrlMapping("packPreList") 
	@JsonResult
	public String packPreList(ServerRequest request,ServerResponse response) {
		String result;
		try {
			result = getSourceModelService().packPreList();
		} catch (Exception e) {
			log.error(e.getMessage(),e);
			result = StatusCode.PARAM_ERROR.toJSON();
		}
		log.info("OUT[" + result + "]");
		return result;
	}
}	